package com.midea.dataStructure;

/**
 * @description：
 * @author: wangwl23
 * @create: 2021-01-01 17:46
 */
public class MyQueue {
    private String[] items;
    private int n = 0;
    //队头下标
    private int head = 0;
    //队尾下标
    private int tail = 0;
    public MyQueue(int capacity){
        items = new String[capacity];
        n = capacity;
    }
    //入队
    public boolean enqueue(String item){
        //队列已满
        if (tail == n){
            if(head == 0){
                return false;
            }else{
                //数据搬移
                for(int i= head;i<= tail;i++){
                    items[i-head] = items[i];
                }
                tail -= head;
                head = 0;
            }
        }
        items[tail] = item;
        ++tail;
        return true;
    }

    //出队
    public String dequeue(){
        //队列为空
        if (head == tail){
            return null;
        }
        String item = items[head];
        ++head;
        return item;
    }
}
